﻿using BaiTu.EventEntity;
using BaiTu.EventServices.Factory;
using BaiTu.EventServices.Helpers;
using BaiTu.Framework.Core.Interfaces;
using BaiTu.IEventServices;
using BaiTu.IQueueService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.EventServices.Providers
{
    class RedisEventBusProvider : IEventBusProvider
    {
        private IQueue queueSrv = BaiTu.IOCServices.EngineContext.Current.Resolve<IQueue>();
        public void Publish(IEvent evt)
        {
            using (var client = RedisFactory.GetRedisClient_Publishable())
            {
                EventInfo info = new EventInfo()
                {
                    SourceClassType = evt.GetType().FullName,
                    SourceAssemblyName = evt.GetType().AssemblyQualifiedName,
                    Xml = EventHelper.GeneratePublishCommand(evt)
                };
             //   client.PublishMessage(info.GetType().FullName, Common.Helpers.XMLSerializationHelper.Serialize(info));
                queueSrv.SendRequestToWorker<EventInfo>(info);
            }
        }

        /// <summary>
        /// 订阅一个事件类型
        /// </summary>
        /// <param name="evtClassType"></param>
        public void Subscribe(string evtClassType)
        {
            using (var client = RedisFactory.GetRedisClient_Subscribe())
            {
                client.Subscribe(evtClassType);
            }
            RedisFactory.TouchReceiveThread();
        }
    }
}
